package gao.xiaolei.controller;

import gao.xiaolei.dto.TestCheckCreateDto;
import gao.xiaolei.dto.TestCheckDetailDto;
import gao.xiaolei.dto.TestCheckDto;
import gao.xiaolei.dto.TestCheckLogDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.service.TestDesignCheckService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/testDesignCheck")
public class TestDesignCheckController {

    @Resource(name = "TestDesignCheckService")
    private TestDesignCheckService testDesignCheckService;

    //添加审核记录
    @PostMapping("")
    public void addCheck(@RequestBody List<TestCheckCreateDto> testDesignChecks) {
        testDesignCheckService.addCheck(testDesignChecks);
    }

    //获取某个项目下某个审核人的审核记录
    @GetMapping("/checkMan/{projectId}/{checkMan}/{status}")
    public List<TestCheckDto> getByCheckManIsFinish(@PathVariable("projectId") String projectId,
                                                    @PathVariable("checkMan") String checkMan,
                                                    @PathVariable("status") Integer status) {
        if (status == 1)//如果status==1就去获取已经审核完的审核记录
            return testDesignCheckService.findByCheckManAndIsFinsh(checkMan, 1, projectId);
        else if (status == 0)//如果status==0就去获取开始审核但是还没审核完的审核记录
            return testDesignCheckService.findByCheckManAndIsFinsh(checkMan, 0, projectId);
        else //如果status等于-1就去拿待审核的测试需求
            return testDesignCheckService.getReadyToCheck(projectId);
    }

    //获取某个测试计划的所有审核记录
    @GetMapping("/common/{testDesignId}")
    public List<TestCheckLogDto> getByTestDesign(@PathVariable("testDesignId") String testDesignId) {
        return testDesignCheckService.getTargetCheckLog(testDesignId);
    }

    //获取审核批注
    @GetMapping("/common/annotate/{id}")
    public String getAnnotate(@PathVariable("id") String id) {
        return testDesignCheckService.getAnnotate(id);
    }

    //修改审核是否通过和审核批注
    @PutMapping("/annoAndIsPass")
    public void updateAnnotateAndIsPass(@RequestBody List<TestCheckCreateDto> testDesignChecks) {
        testDesignCheckService.updateAnnotateAndIsPass(testDesignChecks);
    }

    //获取审核详情
    @GetMapping("/detail/{status}/{id}")
    public TestCheckDetailDto getDetail(@PathVariable("id") String id,
                                        @PathVariable("status") Integer status) {
        return testDesignCheckService.getDetail(id, status);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
